﻿@model Jb.Framework.Common.Forms.JbSignature

<div class="pull-left form-group">
    @{
        var random = new Random();
        var title = Model.Title.Trim();
        title = title.Replace(" ", "");
        var id = title + random.Next(); // maby ilegal character in title make mistake in valid id
    }

    <label>
        @Model.Title
        @if (!string.IsNullOrEmpty(Model.HelpText))
        {
            <a href="#" class="name-info" data-container="body" rel="popover" data-placement="top" data-content="@Model.HelpText"></a>
        }
    </label>

    @{
        string name = string.Format("signaturePad{0}", Guid.NewGuid().ToString().Substring(0, 5));
    }
    <div id="@name" class="signature signatureControls">
        <div class="view-mode-layer"></div>
        <a id="clearSignature" class="clear-sign clearButton">clear</a>
        @Html.HiddenFor(x => x.Value, new { @data_SignatureValue = "true" })
    </div>
    <span class="signatureClear" style="visibility: hidden;">You must sign to proceed.</span>
    @Html.ValidationMessageFor(x => x.HasSigned)
    @Html.HiddenFor(x => x.HasSigned, new { @class = "formValidator", @data_hasSigned = "true", @data_name = name })

    @Html.Hidden("ModelType", Model.GetType())
    @Html.HiddenFor(x => x.HelpText)
    @Html.HiddenFor(x => x.Title)

</div>

<script type="text/javascript">
    $(document).ready(function () {

        // initial popover
        popoverInit();

        //  $('.signature').jSignature({ lineWidth: 1, width: 300, height: 100 });
        $("#" + "@name").each(function () {
            $(this).jSignature({
                lineWidth: 1,
                width: 300,
                height: 100,
                mousedown: function () {
                    //$("[data-hasSigned='true']").val('True')
                }
            });
        });


        $(".clearButton").click(function (event) {
            event.preventDefault();
            $(this).closest(".signatureControls").jSignature('clear');
            $("input[data-SignatureValue='true']").val("");
        });

        @if (string.IsNullOrEmpty(Model.Value) == false)
        {
        <text>
        $("#" + "@name").jSignature("setData", "@(Model.Value)");
        </text>    
        }
    })
</script>


